﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project32CHE.Biz.Goods;
using Project32CHE.Biz.Goods.Extend.Model;
using Project32CHE.Biz.Unionshop;

namespace Project32CHE.Biz.Goods.Extend
{
    public static class PriceService
    {
        /// <summary>
        /// 计算成本价
        /// </summary>
        /// <param name="operate"></param>
        /// <param name="goodsId"></param>
        /// <param name="quantity"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        public static decimal CalculateCostPrice(InventoryOperate operate, Guid goodsId, decimal quantity, decimal price)
        {
            var goods = GoodsService.Get(goodsId);
            var count = InventoryService.GetStorageItem(goods.ShopID, goods.ID).Sum(x => x.Qty);
            var costPrice = 0M;
            switch (operate)
            {
                case InventoryOperate.StoreIn:
                    costPrice = (goods.CostPrice * count + price * quantity) / (count + quantity);
                    break;
                case InventoryOperate.DeliveryOut:
                    costPrice = (count == quantity ? 0 : (goods.CostPrice * count - price * quantity) / (count - quantity));
                    break;
            }
            return costPrice;
        }
        public static decimal CalculateCostPrice(InventoryOperate operate, decimal inventoryQuantity, decimal inventoryCostPrice, decimal quantity, decimal price)
        {
            var costPrice = 0M;
            //if (inventoryCostPrice == price)
            //{
            //    costPrice = inventoryCostPrice;
            //}
            //else
            {
                switch (operate)
                {
                    case InventoryOperate.StoreIn:
                        costPrice = (inventoryQuantity * inventoryCostPrice + quantity * price) / (inventoryQuantity + quantity);
                        break;
                    case InventoryOperate.DeliveryOut:
                        costPrice = (inventoryQuantity == quantity ? 0 : (inventoryQuantity * inventoryCostPrice - quantity * price) / (inventoryQuantity - quantity));
                        break;
                }
            }
            return costPrice;
        }
    }
}
